package com.hanfu.ucenter.web.web.controller;

import com.hanfu.ucenter.web.enums.UcenterEnum;
import com.hanfu.ucenter.web.exception.ErrorCode;
import com.hanfu.ucenter.web.exception.UCenterException;
import com.hanfu.ucenter.web.web.dto.Request.ReqUnionPicUpload;
import com.hanfu.ucenter.web.web.dto.Response.RespUnionPicUpload;
import com.hanfu.ucenter.web.web.param.UserDto;
import com.hanfu.ucenter.web.web.param.union.UnionPicUpload;
import com.hanfu.ucenter.web.web.util.UnionUtil;
import com.hanfu.ucenter.web.web.vo.union.UnionPicResp;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by dufei on 19/9/20.
 */
@RestController
@RequestMapping(method = RequestMethod.POST, value = "/uCenter/UnionPicUpload")
public class UnionPicUploadController extends UCenterBaseController<ReqUnionPicUpload,RespUnionPicUpload>  {

    @Override
    protected RespUnionPicUpload doService(ReqUnionPicUpload req, HttpServletRequest httpServletRequest, UserDto userInfo) throws UCenterException {
        UcenterEnum.uploadType uploadType = UcenterEnum.uploadType.getByValue(req.getType());
        if (uploadType == null) {
            throw new UCenterException(ErrorCode.UPLOAD_TYPE_ERROR);
        }
        UnionPicUpload unionPicUpload = new UnionPicUpload();
        unionPicUpload.setPic_base64(req.getPic());
        UnionPicResp unionPicResp = (UnionPicResp) UnionUtil.execute(unionPicUpload,UnionPicResp.getInstance());
        logger.info("unionPicResp is {}",unionPicResp);
        RespUnionPicUpload res = new RespUnionPicUpload();
        res.setDocument_name(uploadType.getName());
        res.setDocument_type(uploadType.getValue());
        res.setFile_path(unionPicResp.getFile_path());
        res.setFile_size(unionPicResp.getFile_size());
        return res;
    }
}
